package com.example.student_management.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.example.student_management.entity.Student;
import com.example.student_management.service.StudentService;
import com.github.pagehelper.Page;

import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.PostMapping;



// 控制层（接受请求，返回响应）
@Controller // 标记为控制层（处理页面请求，区别于 @RestController）
@RequestMapping("/student") // 统一 URL 前缀（如 /student/list）
public class StudentController {
    
    @Autowired
    private StudentService studentService;

    // 1. 跳转到学生列表
    @GetMapping("/list")
    public String list(@RequestParam(value = "keyword", required = false) String keyword, 
    @RequestParam(value = "pageNum", defaultValue = "1") int pageNum, 
    @RequestParam(value = "pageSize", defaultValue = "5") int pageSize,
    Model model) {

        Page<Student> studentPage;

        if (keyword != null && !keyword.trim().isEmpty()) {
            studentPage = studentService.getStudentsByKeywordAndPage(keyword, pageNum, pageSize);
            // 将关键字回传到前端页面
            model.addAttribute("keyword", keyword);
        }
        else
        {
            studentPage = studentService.getAllStudentsByPage(pageNum, pageSize);
        }

        model.addAttribute("students", studentPage.getResult()); // 获取当前页的数据列表
        model.addAttribute("pageInfo", studentPage); // 将整个 Page 对象传递给前端，方便获取分页信息

        System.out.println(studentPage);
        
        return "student/list"; // 返回 Thymeleaf 页面（resources/templates/student/list.html）
    }

    

    // 2. 跳转到新增页面
    @GetMapping("/toAdd")
    public String toAdd() {
        return "student/add"; // 对应 templates/student/add.html
    }
    
    // 3. 执行新增操作
    @PostMapping("/add")
    public String add(
        @RequestParam("age")
        @Min(value = 13, message = "用户年龄最小13岁")
        @Max(value = 16, message = "用户年龄最大16岁")
        Integer age,
        Student student) {
        studentService.addStudent(student);
        return "redirect:/student/list";  // 新增后重定向到列表页
    }
    
    // 4. 跳转到编辑页
    @GetMapping("/toEdit/{id}")
    public String toEdit(@PathVariable Integer id, Model model) {
        Student student = studentService.getStudentById(id);
        model.addAttribute("student", student);  // 传递待编辑的学生信息
        return "student/edit";  // 对应 templates/student/edit.html
    }
    
    // 5. 执行编辑操作
    @PostMapping("/edit")
    public String edit(Student student) {
        studentService.updateStudent(student);
        return "redirect:/student/list";  // 编辑后重定向到列表页
    }
    
    // 6. 执行删除
    @GetMapping("/delete/{id}")
    public String delete(@PathVariable Integer id) {
        studentService.deleteStudent(id);
        return "redirect:/student/list";
    }
    
    
}
